// static/js/export-service.js
const ExportService = {
    export({type = 'html', content = '', filename = 'note_export'}) {
        switch (type) {
            case 'html':
                this.exportHtml(content, filename);
                break;
            case 'doc':
                this.exportDoc(content, filename);
                break;
            case 'pdf':
                this.exportPdf(content, filename);
                break;
            case 'markdown':
                this.exportMarkdown(content, filename);
                break;
            case 'png':
                this.exportPng(content, filename);
                break;
            default:
                console.warn('未知导出类型:', type);
        }
    },

    exportHtml(html, filename) {
        const htmlContent = `
      <!DOCTYPE html>
      <html>
      <head>
        <meta charset="UTF-8">
        <title>${filename}</title>
        <style>
          body { font-family: sans-serif; padding: 20px; }
          pre code { background: #f8f8f8; padding: 8px; display: block; }
        </style>
      </head>
      <body>${html}</body>
      </html>
    `;
        const blob = new Blob([htmlContent], {type: 'text/html;charset=utf-8'});
        this.downloadBlob(blob, `${filename}.html`);
    },

    exportDoc(html, filename) {
        const docContent = `
      <html xmlns:o='urn:schemas-microsoft-com:office:office'
            xmlns:w='urn:schemas-microsoft-com:office:word'
            xmlns='http://www.w3.org/TR/REC-html40'>
      <head><meta charset='utf-8'><title>${filename}</title></head>
      <body>${html}</body>
      </html>
    `;
        const blob = new Blob([docContent], {type: 'application/msword;charset=utf-8'});
        this.downloadBlob(blob, `${filename}.doc`);
    },

    exportPdf(domElement, filename) {
        const opt = {
            margin: 0.5,
            filename: `${filename}.pdf`,
            image: {type: 'jpeg', quality: 0.98},
            html2canvas: {scale: 2},
            jsPDF: {unit: 'in', format: 'a4', orientation: 'portrait'}
        };
        html2pdf().set(opt).from(domElement).save();
    },

    exportPng(domElement, filename) {
        if (!domElement) {
            console.warn('缺少 DOM 元素，无法导出图片');
            return;
        }

        html2canvas(domElement, {
            scale: 2,
            useCORS: true
        }).then(canvas => {
            canvas.toBlob(blob => {
                if (blob) {
                    this.downloadBlob(blob, `${filename}.png`);
                }
            });
        }).catch(err => {
            console.error('导出 PNG 失败:', err);
        });
    },

    exportMarkdown(md, filename) {
        const blob = new Blob([md], {type: 'text/markdown;charset=utf-8'});
        this.downloadBlob(blob, `${filename}.md`);
    },

    downloadBlob(blob, filename) {
        const a = document.createElement('a');
        a.href = URL.createObjectURL(blob);
        a.download = filename;
        a.click();
    }
};

window.ExportService = ExportService;
